package cn.ccbom.bom.interview.util.script.script;

import cn.ccbom.bom.interview.util.script.task.TaskItem;
import cn.hutool.core.io.file.PathUtil;
import cn.hutool.core.io.resource.ResourceUtil;
import cn.hutool.core.net.NetUtil;
import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.extra.spring.SpringUtil;
import jdk.internal.dynalink.beans.StaticClass;
import jdk.nashorn.api.scripting.NashornScriptEngineFactory;
import lombok.experimental.UtilityClass;

import javax.script.SimpleBindings;
import java.util.HashMap;
import java.util.concurrent.ExecutorService;
import java.util.stream.Collectors;

@UtilityClass
public class EngineUtil {
    public static void main(String[] args) {
        EngineClient engine = EngineUtil.createEngine();
        //各自为政
        Object 测试 = engine.executeUrl("测试", "classpath:hello.js");

        Object getPhone = engine.invoke("ip0");
        //TaskItem item = new TaskItem("测试");
        //item.setUrl("classpath:hello.js");
        //item.setScript("ThreadUtil.sleep(10000);a=1+4;b=2;this['name']='ccbom';ttt=1");
       // Object run = item.run(engine);
        System.out.println("===========");
    }

    private static final class NashornScriptEngineFactoryHolder {
        static final NashornScriptEngineFactory factory = new NashornScriptEngineFactory();
        static final SimpleBindings globalBindings = new SimpleBindings(new HashMap<String, Object>() {{
            //todo 添加常见的依赖库
            put("NetUtil", StaticClass.forClass(NetUtil.class));
            put("SpringUtil", StaticClass.forClass(SpringUtil.class));
            put("ThreadUtil", StaticClass.forClass(ThreadUtil.class));
            put("Collectors", StaticClass.forClass(Collectors.class));
        }});
    }

    public static NashornScriptEngineFactory getNashornScriptEngineFactory() {
        return NashornScriptEngineFactoryHolder.factory;
    }

    public static SimpleBindings globalBindings() {
        return NashornScriptEngineFactoryHolder.globalBindings;
    }

    public EngineClient createEngine() {
        return new EngineClient();
    }

//
//    public void createEngine() {
//        //__noSuchMethod__: 找不到对象方法的函数
//        //__noSuchProperty__: 找不到属性的函数
//        //属性赋值: Object.setPrototypeOf(obj, newProto)
//        // bind properties of 'obj2' to 'obj' Object.bindProperties(obj, obj2);
//        //Object.bindProperties(this, obj2);
//        //__FILE__ , __LINE__, __DIR__
//        //Packages, java, javax, javafx, com, edu, org -> 可以简写 不用谢Packages
//        //var imports = new JavaImporter(java.util, java.io); with(imports) {}
//        //内部类 var ftype = Java.type("java.awt.geom.Arc2D$Float") or Java.type("java.awt.geom.Arc2D.Float")
//        // var Class = Java.type("java.lang.Class");var VectorClass = Class.forName("java.util.Vector");var Vector = VectorClass.static;
//        //var ArrayList = Java.type("java.util.ArrayList");var ArrayListExtender = Java.extend(ArrayList)
//        //Java array to JavaScript array conversion by Java.from
//        // Java.to used to convert script array to a Java int array
//        //Java.synchronized(function() sum++, lock)();
//        //java.lang.System.out["println(int)"](Math.PI); 指定方法,类型限制
//        //java.awt["Color(int,int,int)"]; 构造方法
////        LinkedHashMap<String, String> database = new LinkedHashMap<>();
////        database.put("A", "function add(b,d){return b+d;}");
////        database.put("B", "function add(b,d){return b*d;}");
////        //全局唯一
////        } catch (Exception e) {
////            throw new IllegalArgumentException(e);
////        }
//    }
}
